package com.clementheliou.cheliou.dal.model.comment;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.annotations.Type;
import org.joda.time.LocalDateTime;

import com.clementheliou.cheliou.dal.model.base.BaseEntity;
import com.clementheliou.cheliou.dal.model.member.Member;
import com.clementheliou.cheliou.dal.model.photo.Photo;

/**
 * Entity dedicated to the comment data.
 * 
 * @author Clément HELIOU (clement.heliou@gmail.com)
 * @see {@link BaseEntity};
 * @see {@link Entity};
 * @see {@link Serializable};
 * @see {@link Table}.
 * @since 1.0
 */
@Entity
@Table(name = "comment")
public class Comment extends BaseEntity implements Serializable {

	/**
	 * UID for serialization.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	private static final long serialVersionUID = -6017365959372048735L;

	/**
	 * The date of the comment.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	private LocalDateTime additionDate;

	/**
	 * The author of the comment.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link Member}.
	 * @since 1.0
	 */
	private Member author;

	/**
	 * The photo the comment is related to.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link Photo}.
	 * @since 1.0
	 */
	private Photo commentedPhoto;

	/**
	 * The comment content.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	@Basic(optional = false)
	@Column(length = 8000, nullable = false)
	@NotNull
	@Size(max = 8000)
	private String content;

	/*
	 * Getters & setters.
	 */

	@GeneratedValue(generator = "seq_comment")
	@Id
	@SequenceGenerator(name = "seq_comment", sequenceName = "seq_comment")
	public Integer getId() {
		return id;
	}

	@Basic(optional = false)
	@Column(name = "date", nullable = false)
	@NotNull
	@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
	public LocalDateTime getAdditionDate() {
		return additionDate;
	}

	@Transient
	public Date getAdditionJdkDate() {
		return additionDate.toDate();
	}

	public void setAdditionDate(LocalDateTime additionDate) {
		this.additionDate = additionDate;
	}

	@JoinColumn(name = "memberid", nullable = false)
	@ManyToOne
	@NotNull
	public Member getAuthor() {
		return author;
	}

	public void setAuthor(Member author) {
		this.author = author;
	}

	@JoinColumn(name = "photoid", nullable = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@NotNull
	public Photo getCommentedPhoto() {
		return commentedPhoto;
	}

	public void setCommentedPhoto(Photo commentedPhoto) {
		this.commentedPhoto = commentedPhoto;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
}
